﻿<%@ Page Language="VB"%>

<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.SessionState" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.Page" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Web.UI.WebControls.WebParts" %>
<%@ Import Namespace="System.Web.UI.HtmlControls" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Collections.Generic" %>


<!-- main container -->
<div id="SolidQCaptcha-Container" class="SolidQCaptcha-Container">
    <!-- left zone -->
    <div id="SolidQCaptcha-LeftZone" class="SolidQCaptcha-LeftZone">
        <!-- text zone-->
        <div id="SolidQCaptcha-TextZone" class="SolidQCaptcha-TextZone">
            <%= ""%>
            <% Dim r As New Random%>
            <%  Select Case language
                    Case "english"%>
                        <%= en_text_1 %><span style="font-weight:bolder; font-size:20px; color:<%= GetNameColour(r.Next(0, 8), "English") %>"><%= GetNameColour(keyColour, "English")%>
                        <br/></span><%=en_text_2%>
                    <%Case "spanish"%>
                        <%= es_text_1%><br/>
                        <span style="font-weight:bolder; font-size:20px; color:<%= GetNameColour(r.Next(0, 8), "English") %>"><%= GetNameColour(keyColour, "Spanish")%></span>&nbsp;<%= es_text_2%>
                    <%Case Else%>
                        <%= en_text_1 %><span style="font-weight:bolder; font-size:20px; color:<%= GetNameColour(r.Next(0, 8), "English") %>"><%= GetNameColour(keyColour, "English")%>
                        <br/></span><%=en_text_2%>
            <%End Select%>                
        </div>
        <div style="clear:both;"><!-- --></div>
        <!-- colours zone -->
        <div id="SolidQCaptcha-ColoursContainer" class="SolidQCaptcha-ColoursContainer">
            <% Dim key As Integer = r.Next(0, ncolours - 1)%>
            <%  For i As Integer = 0 To ncolours - 1%>
                <%If key = i Then%>
                    <div id="SolidQCaptcha-Draggable-<%=Session(SolidQCaptchaCode) %>" class="SolidQCaptcha-ColourX ui-widget-content" style="background:transparent url('/_layouts/Images/SolidMSSControls/Captcha/squares/<%= keyColour %>.jpg');" >
                <%Else%>
                    <div id="SolidQCaptcha-Draggable-<%=GetCode() %>" class="SolidQCaptcha-ColourX ui-widget-content" style="background:transparent url('/_layouts/Images/SolidMSSControls/Captcha/squares/<%= GetRandomColor() %>.jpg');">
                <%End If%>
                </div>                    
            <% Next%>
            <a href="javascript:ResetColors()">
                <img src="/_layouts/Images/SolidQ.MSS.Controls/Captcha/reload_icon.png" alt=""/>
            </a>
        </div>

    </div>
    <!-- right zone -->
    <div id="SolidQCaptcha-RightZone" class="SolidQCaptcha-RightZone">
        <div id="SolidQCaptcha-BlankZone" class="SolidQCaptcha-BlankZone ui-widget-header">
            <!-- -->
        </div>
    </div>  
</div>

<script language="VB" runat="server">
    
    'Session variables
    Const SolidQCaptchaCode As String = "SolidQCaptchaCode"
    
    'Texts
    'English language
    Const en_text_1 As String = "Drag &amp; Drop the "
    Const en_text_2 As String = "square inside the circle on the right."
    'Spanish language
    Const es_text_1 As String = "Arrastra y suelta el cuadrado"
    Const es_text_2 As String = "dentro del círculo. "
    
    'Auxiliar list, to don't repeat colours    
    Dim coloursList As List(Of Integer) = New List(Of Integer)
    
    'Colors
    Enum colours
        red
        green
        yellow
        blue
        pink
        brown
        purple
        orange
        black
        gray
    End Enum
    
    'Key colour
    Dim keyColour As Integer = 0
    
    'Get params from querystring
    Dim ncolours As Integer = GetNCQS()
    Dim language As String = GetLQS()
       
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        
        'Create session variables
        InitSV()
        
        'Remove all headers and data in the Response.
        Response.Clear()
        'Set the mime type        
        Response.ContentType = "text/xml"
        'Buffer output and send it out in one chunk.
        Response.BufferOutput = True
        'Specify a succesful HTTP status code
        Response.StatusCode = 200
        
    End Sub
    
    Function GetNCQS() As Integer
        Dim n As String = HttpContext.Current.Request.QueryString("ncolours")
        If String.IsNullOrEmpty(n) Then
            'Number of colours by default
            Return 5
        Else
            Return CInt(n)
        End If
    End Function
    
    Function GetLQS() As String
        Dim l As String = HttpContext.Current.Request.QueryString("language")
        If String.IsNullOrEmpty(l) Then
            'Language by default
            Return "english"
        Else
            Return l
        End If
    End Function
    
    Function GetCode() As String
        Return Guid.NewGuid().ToString.Replace("-", String.Empty)
    End Function
    
    Sub InitSV()
        
        'First we build the key colour
        keyColour = GetRandomColor()
        If coloursList.Count = 0 Then
            'coloursList.Add(DirectCast([Enum].Parse(GetType(colours), keyColour), colours))
            coloursList.Add(keyColour)
        End If
        
        'Then we create the guid code
        If (Session(SolidQCaptchaCode) Is Nothing) Then
            Session.Add(SolidQCaptchaCode, GetCode())
        Else
            Session(SolidQCaptchaCode) = GetCode()
        End If
    End Sub
    
    Function GetRandomColor() As Integer
        Dim rand As New Random
        Dim random_color As Integer
        
        Do
            random_color = rand.Next(0, 9)
        Loop While (coloursList.Contains(random_color))
        
        coloursList.Add(random_color)

        Return random_color
        
    End Function
    
    
    Function GetNameColour(ByVal n As Integer, ByVal l As String) As String
        
        Dim r As String = String.Empty
        
        Select Case n
            Case 0
                If l = "Spanish" Then
                    r = "Blanco"
                Else
                    r = "White"
                End If
            Case 1
                If l = "Spanish" Then
                    r = "Rojo"
                Else
                    r = "Red"
                End If
                
            Case 2
                If l = "Spanish" Then
                    r = "Verde"
                Else
                    r = "Green"
                End If
            Case 3
                If l = "Spanish" Then
                    r = "Amarillo"
                Else
                    r = "Yellow"
                End If
            Case 4
                If l = "Spanish" Then
                    r = "Azul"
                Else
                    r = "Blue"
                End If
            Case 5
                If l = "Spanish" Then
                    r = "Rosa"
                Else
                    r = "Pink"
                End If
            Case 6
                If l = "Spanish" Then
                    r = "Marrón"
                Else
                    r = "Brown"
                End If
            Case 7
                If l = "Spanish" Then
                    r = "Violeta"
                Else
                    r = "Purple"
                End If
            Case 8
                If l = "Spanish" Then
                    r = "Naranja"
                Else
                    r = "Orange"
                End If
            Case 9
                If l = "Spanish" Then
                    r = "Negro"
                Else
                    r = "Black"
                End If
            Case Else
                If l = "Spanish" Then
                    r = "Gris"
                Else
                    r = "Gray"
                End If
        End Select
        
        Return r
        
    End Function
    
</script>